import React from 'react';
import { format } from '@/utils/datetime';
import { renderNumber } from '@/utils/common';
import CommonPage from '@/components/page/CommonPage';
import { useModel } from 'umi';
const daoName = 'ShAsseFyzj';
const importName = 'ShAsseFyzj';
const columns: API.Column[] = [
  {
    title: '操作人',
    width: 120,
    sorter: true,
    dataIndex: 'CZR',
    key: 'CZR',
    dispaly: true,
    filter: false,
    export: false,
  },
  {
    title: '操作日期',
    width: 120,
    sorter: true,
    key: 'CZRQ',
    dataIndex: 'CZRQ',
    render: (data: any) => format(data, 'yyyy-MM-dd'),
    dispaly: true,
    filter: false,
    export: false,
  },
  {
    title: '日期',
    width: 100,
    sorter: true,
    key: 'DTID',
    dataIndex: 'DTID',
    render: (data: any) => format(data, 'yyyy-MM-dd'),
    dispaly: true,
    filter: true,
    canEdit: true,
    export: true,
  },
  {
    title: '部门',
    width: 100,
    sorter: true,
    key: 'UTNM',
    dataIndex: 'UTNM',
    dispaly: true,
    filter: false,
  },
  {
    title: '员工',
    width: 100,
    sorter: true,
    key: 'EMNM',
    dataIndex: 'EMNM',
    dispaly: true,
    filter: true,
    export: true,
    dataType: 'string',
  },
  {
    title: '考核参数',
    width: 150,
    sorter: true,
    key: 'PARAID',
    dataIndex: 'KHCS',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '利润增减额',
    width: 150,
    className: 'column-money',
    sorter: true,
    dataIndex: 'LREJ',
    render: (data) => renderNumber('.2f', data),
    key: 'LREJ',
    dispaly: true,
    align: 'right',
    filter: true,
    export: true,
  },
  {
    title: '绩效增减额',
    width: 150,
    className: 'column-money',
    sorter: true,
    dataIndex: 'JXEJ',
    render: (data) => renderNumber('.2f', data),
    key: 'JXEJ',
    align: 'right',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '费用增减额',
    width: 150,
    lassName: 'column-money',
    sorter: true,
    dataIndex: 'FYEJ',
    render: (data) => renderNumber('.2f', data),
    key: 'FYEJ',
    align: 'right',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '说明',
    width: 600,
    sorter: true,
    dataIndex: 'EXPL',
    key: 'EXPL',
    dispaly: true,
    filter: true,
    export: true,
  },
];
const controlsImport: API.Control[] = [
  { name: 'DTID', dataType: 'date', type: 'date', label: '日期', grid: 12 / 24 },
  { name: 'UTNM', type: 'text', label: '部门', grid: 12 / 24 },
  { name: 'EMNM', type: 'text', label: '员工', grid: 12 / 24 },
  { name: 'KHCS', type: 'text', label: '业务类型', grid: 12 / 24 },
  { name: 'JSJF', type: 'text', label: '自动计算', grid: 12 / 24 },
  {
    name: 'LREJ',
    label: '利润增减额',
    rules: [{ required: false, message: '不能为空' }],
    type: 'number',
    dataType: 'number',
    grid: 12 / 24,
  },
  {
    name: 'JXEJ',
    label: '绩效增减额',
    rules: [{ required: false, message: '不能为空' }],
    type: 'number',
    dataType: 'number',
    grid: 12 / 24,
  },
  {
    name: 'FYEJ',
    label: '费用增减额',
    rules: [{ required: false, message: '不能为空' }],
    type: 'number',
    dataType: 'number',
    grid: 12 / 24,
  },
  {
    name: 'EXPL',
    label: '说明',
    type: 'text',
    grid: 24 / 24,
    layout: { labelSpan: 3, wrapperSpan: 20 },
  },
];
const ShAsseFyzj: React.FC = () => {
  const { user, edwDate } = useModel('@@initialState', (ret) => {
    return {
      user: ret.initialState?.user,
      edwDate: ret.initialState?.sys?.edwDate,
    };
  });
  const controls: API.Control[] = [
    {
      name: 'DTID',
      rules: [{ required: true, message: '日期不能为空' }],
      label: '日期',
      type: 'date',
      grid: 12 / 24,
      dataType: 'date',
    },
    {
      name: 'EMID',
      label: '员工',
      type: 'search',
      width: 300,
      grid: 12 / 24,
      rules: [{ required: true, message: '员工不能为空' }],
      dataType: 'number',
      fetch: {
        tableName: 'v_user_name',
        searchColumns: ['EMNO', 'EMNM'],
        optionTpl: 'NAME',
        valueTpl: 'ID',
        isDao: false,
      },
    },
    {
      name: 'PARAID',
      label: '考核参数',
      type: 'select',
      grid: 12 / 24,
      rules: [{ required: true, message: '考核参数不能为空' }],
      dataType: 'number',
      fetch: {
        tableName: 'ShAsseParam',
        optionTpl: 'NAME',
        valueTpl: 'ID',
        sorter: [{ FieldName: 'STNO', Direction: 'ASC' }],
        isDao: true,
      },
    },
    {
      name: 'LREJ',
      label: '利润增减',
      rules: [{ required: true, message: '利润增减不能为空' }],
      type: 'number',
      dataType: 'number',
      grid: 12 / 24,
    },
    {
      name: 'JSJF',
      label: '计算绩费',
      rules: [{ required: true, message: '计算绩费不能为空' }],
      options: [
        { value: '1', label: '是' },
        { value: '0', label: '否' },
      ],
      grid: 12 / 24,
      type: 'select',
      initValue: '1',
    },
    {
      name: 'JXEJ',
      label: '绩效增减',
      rules: [{ required: false, message: '绩效增减能为空' }],
      type: 'number',
      dataType: 'number',
      grid: 12 / 24,
      disabled: false,
    },
    {
      name: 'FYEJ',
      label: '费用增减',
      rules: [{ required: false, message: '费用增减能为空' }],
      type: 'number',
      dataType: 'number',
      grid: 12 / 24,
      disabled: false,
    },
    {
      name: 'EXPL',
      label: '说明',
      type: 'text',
      grid: 24 / 24,
      layout: { labelSpan: 3, wrapperSpan: 20 },
    },
  ];
  const options: API.OptionItem[] = [];
  const curdate = new Date(edwDate!);
  const year = curdate.getFullYear();
  for (let i = year; i >= year - 10; i--) {
    options.push({ label: `${i}年`, value: i });
  }
  const options2: API.OptionItem[] = [
    { label: '全年', value: 0 },
    { label: '一季度', value: 1 },
    { label: '二季度', value: 2 },
    { label: '三季度', value: 3 },
    { label: '四季度', value: 4 },
  ];
  const m = curdate.getMonth() + 1;
  const d = curdate.getDate();
  let sjmDate;
  if ((m == 3 && d !== 31) || m === 1 || m === 2) {
    const sy = year - 1;
    sjmDate = new Date('' + sy + '/12/31');
  } else if ((m == 6 && d !== 30) || m === 4 || m === 5 || (m === 3 && d === 31)) {
    sjmDate = new Date('' + year + '/03/31');
  } else if ((m == 9 && d !== 30) || m === 7 || m === 8 || (m == 6 && d == 30)) {
    sjmDate = new Date('' + year + '/06/30');
  } else if ((m == 12 && d !== 31) || m === 10 || m === 11 || (m == 9 && d == 30)) {
    sjmDate = new Date('' + year + '/09/30');
  } else {
    sjmDate = new Date('' + year + '/12/31');
  }
  return (
    <CommonPage
      daoName={daoName}
      columns={columns}
      controls={controls}
      extraVals={{ CZRID: user?.userID, CZR: user?.name }}
      title="绩费增减"
      importName={importName}
      importControls={controlsImport}
      importMode="InsertAndUpdate"
      importParamNames={['DTID']}
      importKeys={['UTNM', 'EMNM', 'EXPL']}
      importParamVals={{ CZRID: '' + user?.userID, CZR: user?.name }}
      importTemplateName="绩费增减导入模板"
      initSorter={{ FieldName: 'FYID', Direction: 'DESC' }}
      copyAdd={true}
      multiSelect={true}
      canRecompute={true}
      copyAddInitVals={{ DTID: sjmDate }}
      shortcutFilters={{
        controls: [
          {
            name: 'NF',
            label: '年份',
            type: 'select',
            options,
            dataType: 'number',
            initValue: year,
          },
          {
            name: 'JD',
            label: '季度',
            type: 'select',
            options: options2,
            dataType: 'number',
            initValue: 0,
          },
        ],
        convert: (vals: Record<string, any>) => {
          const nf = '' + vals.NF;
          let sdate: string = '';
          let edate: string = '';
          if (vals.JD === 0) {
            sdate = nf + '-01-01';
            edate = nf + '-12-31';
          } else if (vals.JD === 1) {
            sdate = nf + '-01-01';
            edate = nf + '-03-31';
          } else if (vals.JD === 2) {
            sdate = nf + '-04-01';
            edate = nf + '-06-30';
          } else if (vals.JD === 3) {
            sdate = nf + '-07-01';
            edate = nf + '-09-30';
          } else if (vals.JD === 4) {
            sdate = nf + '-10-01';
            edate = nf + '-12-31';
          }
          return [
            {
              WhereItems: [
                { FieldName: 'DTID', Operator: '>=', FieldValues: sdate },
                { FieldName: 'DTID', Operator: '<=', FieldValues: edate },
              ],
              RelationSign: 'AND',
            },
          ];
        },
      }}
    />
  );
};
export default ShAsseFyzj;
